/*
	Copyright (c) 2008 Memorphic Ltd. All rights reserved.
	
	Redistribution and use in source and binary forms, with or without 
	modification, are permitted provided that the following conditions 
	are met:
	
		* Redistributions of source code must retain the above copyright 
		notice, this list of conditions and the following disclaimer.
	    	
	    * Redistributions in binary form must reproduce the above 
	    copyright notice, this list of conditions and the following 
	    disclaimer in the documentation and/or other materials provided 
	    with the distribution.
	    	
	    * Neither the name of MEMORPHIC LTD nor the names of its 
	    contributors may be used to endorse or promote products derived 
	    from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package memorphic.slide
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import memorphic.core.ICommand;
	
	import mx.resources.ResourceManager;
	
	public class UIAction implements IUIAction
	{
		 
		private var _keyboardShortcut:String;
		private var _label:String;
		private var _tooltipText:String;
		private var _enabled:Boolean = true;
		private var _resourceId:String;
		private var _resourceBundleName:String;
		
		protected var childCommand:ICommand;
		
		
		public static const NULL:UIAction = new UIAction();
		
		
		public function UIAction(childCommand:ICommand=null)
		{
			this.childCommand = childCommand;
			setDefaultLocalizationSettings();
		}


		

		[Bindable]
		public function get keyboardShortcut():String
		{
			return _keyboardShortcut || getLocaleText("keyboardShortcut");
		}
		
		public function set keyboardShortcut(keys:String):void
		{
			_keyboardShortcut = keys;
		}
		
		[Bindable]
		public function get label():String
		{
			return _label || getLocaleText("label");
		}
		
		
		public function set label(v:String):void
		{
			_label = v;
		}
		
		
		[Bindable]
		public function get tooltipText():String
		{
			return _tooltipText || getLocaleText("tooltipText");
		}
		
		public function set tooltipText(text:String):void
		{
			_tooltipText = text;
		}


		[Bindable("__noChangeEvent__")]
		public function get resourceId():String
		{
			return _resourceId;
		}
		public function set resourceId(id:String):void
		{
			_resourceId = id;
		}


		[Bindable("__noChangeEvent__")]
		public function get resourceBundleName():String
		{
			return _resourceBundleName;
		}
		public function set resourceBundleName(bundleName:String):void
		{
			_resourceBundleName = bundleName;
		}


		[Bindable]
		public function get enabled():Boolean
		{
			return _enabled;
		}
		public function set enabled(enable:Boolean):void
		{
			_enabled = enable;
		}


		/**
		 * The same as execute, but the event that triggered the command should be passed. This allows the command to modify
		 * it's behaviour according to how it was triggered. For example, if it is triggered by a MouseEvent or KeyboardEvent, it might give a different notification than if
		 * the event is a TimerEvent.
		 * 
		 * The event can also be used for logging purposes.
		 * 
		 * Implementations of IUIAction should not rely on this method to work properly; execute() should be sufficient, but executeWithEvent() can provide additional or modified behaviour.
		 * 
		 * @param event
		 * 
		 */	
		public function executeWithEvent( event:Event ):void
		{
			execute();
		}
		
		

		/**
		 * 
		 * 
		 */
		public function execute():void
		{
			if(childCommand && enabled){
				childCommand.execute();
			}
		}


		
		
		
		private function getLocaleText(name:String):String
		{
			if(resourceBundleName == null){
				return undefined;
			} 
			var resourceName:String = resourceId + "." + name;
			return ResourceManager.getInstance().getString(resourceBundleName, resourceName);
		}
		
		
		
		private function setDefaultLocalizationSettings():void
		{
			var childAction:IUIAction = childAction as IUIAction;
			if(childAction){
				resourceBundleName = childAction.resourceBundleName;
				resourceId = childAction.resourceId;
			}
			
			var cls:String = getQualifiedClassName(this);
			if(!resourceId){
				resourceId = cls.match(/\w*$/)[0];
			}
			if(!resourceBundleName){
				resourceBundleName = cls.match(/\.(?P<pckg>\w*)\:\:/)["pckg"];
			}
		}
		
	}
}